﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Derbster.Domain;

namespace Derbster.Data
{
    /// <summary>
    /// This is an in-memory version of repository. Note, we don't use the singleton pattern here. 
    /// Instead, we count on Unity to control the lifecycle of these objects. By default, we use
    /// a container-controlled lifecycle, so only one instance of a repository is returned per container.
    /// </summary>
    public class DbTeamRepository : ITeamRepository, IDisposable
    {
        private readonly DerbsterDbContext _dbContext;

        public DbTeamRepository()
        {
            _dbContext = new DerbsterDbContext();
        }

        public Team Create(string name)
        {
            Team newTeam = new Team(name);
            _dbContext.Teams.Add(newTeam);
            _dbContext.SaveChanges();

            return newTeam;
        }

        public Team Create(string name, string location)
        {
            Team newTeam = new Team(name,location);
            _dbContext.Teams.Add(newTeam);
            _dbContext.SaveChanges();

            return newTeam;
        }

        public Team FindById(int id)
        {
            return _dbContext.Teams.SingleOrDefault(m => m.TeamId == id);
        }

        public Team FindByName(string name)
        {
            return _dbContext.Teams.SingleOrDefault(m => m.Name.ToLower() == name.ToLower());
        }

        public IEnumerable<Team> FindByLocation(string location)
        {
            return _dbContext.Teams.Where(m => !String.IsNullOrWhiteSpace(m.Location) && m.Location.ToLower() == location.ToLower()).AsEnumerable();
        }

        public Team Update(Team team)
        {
            _dbContext.SaveChanges();
            return team;
        }

        public void Delete(Team team)
        {
            _dbContext.Teams.Remove(team);
            _dbContext.SaveChanges();
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing && _dbContext != null)
                _dbContext.Dispose();
        }

        public IEnumerator<Team> GetEnumerator()
        {
            return _dbContext.Teams.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Expression Expression
        {
            get
            {
                return _dbContext.Teams.Expression;
            }
        }

        public Type ElementType
        {
            get
            {
                return _dbContext.Teams.ElementType;
            }
        }

        public IQueryProvider Provider
        {
            get
            {
                return _dbContext.Teams.Provider;
            }
        }
    }
}
